﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;
using WCYCReader.EncodingHelper;
using WCYCReader.Main;
using WCYCReader.Models;
using WCYCReader.Resources;
using WCYCReader.ShareFolder;
using WCYCReader.ShareFolder.ViewModel;
using WCYCReader.StorageHelper;

namespace WCYCReader.Pages
{
    public partial class ExplorerPage : PhoneApplicationPage
    {
        #region 变量
        private bool _newPage = true;
        private aa _smb;
        private string _service;
        private GClass7 _remotePathManager = new GClass7(), _localPathManager = new GClass7(), _skyPathManager = new GClass7();
        private List<GClass1> _localList = new List<GClass1>(), _remoteList = new List<GClass1>();
        private GClass1 _fileToRename;
        private List<GClass2> _skyList;
        private GClass0 _clipboard;
        private GClass4 _pm;
        private GClass3 _dm;
        private IsolatedStorageFile _isolatedStore = IsolatedStorageFile.GetUserStoreForApplication();

        private bool _iscopying;
        private bool _isLocalMode;
        private GClass5 _skyDriveManager;
        private bool _IsSignButtonClicked;
        private string _IsTrial;
        #endregion 变量

        #region ******************************内部类******************************

        private sealed class Class1
        {
            public ExplorerPage explorer_0;
            public string string_0;
            private static Func<string, string> func_0;
            private static Func<string, string> func_1;
            public void method_0(object sender, DoWorkEventArgs e)
            {
                IEnumerable<string> arg_48_0 = this.explorer_0._isolatedStore.GetDirectoryNames("content\\" + this.string_0 + "\\*");
                if (ExplorerPage.Class1.func_0 == null)
                {
                    ExplorerPage.Class1.func_0 = new Func<string, string>(ExplorerPage.Class1.smethod_0);
                }
                IOrderedEnumerable<string> orderedEnumerable = Enumerable.OrderBy<string, string>(arg_48_0, ExplorerPage.Class1.func_0);
                IEnumerable<string> arg_90_0 = this.explorer_0._isolatedStore.GetFileNames("content\\" + this.string_0 + "\\*");
                if (ExplorerPage.Class1.func_1 == null)
                {
                    ExplorerPage.Class1.func_1 = new Func<string, string>(ExplorerPage.Class1.smethod_1);
                }
                IOrderedEnumerable<string> orderedEnumerable2 = Enumerable.OrderBy<string, string>(arg_90_0, ExplorerPage.Class1.func_1);
                this.explorer_0._localList.Clear();
                if (this.string_0 != null)
                {
                    GClass1 gClass = new GClass1();
                    gClass.FileName = "..";
                    gClass.ImageSrc = "/Images/folder.png";
                    gClass.IsDirectory = true;
                    gClass.Path = this.string_0;
                    this.explorer_0._localList.Add(gClass);
                }
                using (IEnumerator<string> enumerator = orderedEnumerable.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        string current = enumerator.Current;
                        GClass1 gClass2 = new GClass1();
                        gClass2.FileName = current;
                        gClass2.ImageSrc = "/Images/folder.png";
                        gClass2.IsDirectory = true;
                        gClass2.Path = this.string_0;
                        this.explorer_0._localList.Add(gClass2);
                    }
                }
                using (IEnumerator<string> enumerator2 = orderedEnumerable2.GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        string current2 = enumerator2.Current;
                        GClass1 gClass3 = new GClass1();
                        gClass3.FileName = current2;
                        gClass3.ImageSrc = "/Images/file.png";
                        gClass3.Path = this.string_0;
                        gClass3.FileSize = this.explorer_0.method_9("content\\" + this.string_0 + "\\" + current2);
                        this.explorer_0._localList.Add(gClass3);
                    }
                }
            }
            public void method_1(object sender, RunWorkerCompletedEventArgs e)
            {
                this.explorer_0.txtLocalLocation.Text = (this.string_0);
                this.explorer_0.lstBoxLocal.ItemsSource = (this.explorer_0._localList);
                this.explorer_0.pivotLocation.IsEnabled = (true);
            }
            private static string smethod_0(string string_1)
            {
                return string_1;
            }
            private static string smethod_1(string string_1)
            {
                return string_1;
            }
        }

        private sealed class Class2
        {
            public ExplorerPage explorer_0;
            public PhotoResult photoResult_0;
        }

        private sealed class Class3
        {
            private sealed class Class4
            {
                private sealed class Class5
                {
                    public ExplorerPage.Class3.Class4 class4_0;
                    public ExplorerPage.Class3 class3_0;
                    public ExplorerPage.Class2 class2_0;
                    public Exception exception_0;
                    public void method_0()
                    {
                        MessageBox.Show(this.exception_0.Message);
                    }
                }
                public ExplorerPage.Class3 class3_0;
                public ExplorerPage.Class2 class2_0;
                public Stream stream_0;
                public string string_0;
                public void method_0(object sender, DoWorkEventArgs e)
                {
                    Action action = null;
                    try
                    {
                        Dispatcher arg_23_0 = this.class2_0.explorer_0.Dispatcher;
                        if (action == null)
                        {
                            action = new Action(this.method_1);
                        }
                        arg_23_0.BeginInvoke(action);
                        if (this.class3_0.genum0_0 != GEnum0.const_2)
                        {
                            byte[] array = new byte[4096];
                            int num;
                            while ((num = this.class2_0.photoResult_0.ChosenPhoto.Read(array, 0, array.Length)) > 0)
                            {
                                this.stream_0.Write(array, 0, num);
                            }
                            this.stream_0.Dispose();
                        }
                        else
                        {
                            this.class2_0.explorer_0._skyDriveManager.method_13(this.class2_0.explorer_0._skyPathManager.method_4(), this.string_0, this.class2_0.photoResult_0.ChosenPhoto);
                        }
                    }
                    catch (Exception exception_)
                    {
                        ExplorerPage.Class3.Class4.Class5 @class = new ExplorerPage.Class3.Class4.Class5();
                        @class.class4_0 = this;
                        @class.class3_0 = this.class3_0;
                        @class.class2_0 = this.class2_0;
                        @class.exception_0 = exception_;
                        this.class2_0.explorer_0.Dispatcher.BeginInvoke(new Action(@class.method_0));
                    }
                }
                private void method_1()
                {
                    this.class2_0.explorer_0.pivotLocation.IsEnabled = (false);
                    this.class2_0.explorer_0.ApplicationBar.IsVisible = false;
                    this.class2_0.explorer_0.pbReading.Visibility = (Visibility)(0);
                    this.class2_0.explorer_0.pbReading.IsIndeterminate = true;
                }
            }
            public ExplorerPage.Class2 class2_0;
            public GEnum0 genum0_0;
            public BackgroundWorker backgroundWorker_0;
            public void method_0(object sender, EventArgs e)
            {
                RunWorkerCompletedEventHandler runWorkerCompletedEventHandler = null;
                if (string.IsNullOrEmpty(this.class2_0.explorer_0.txtFileName.Text))
                {
                    MessageBox.Show("Please, enter a file name.");
                    return;
                }
                try
                {
                    ExplorerPage.Class3.Class4 @class = new ExplorerPage.Class3.Class4();
                    @class.class3_0 = this;
                    @class.class2_0 = this.class2_0;
                    @class.stream_0 = null;
                    @class.string_0 = this.class2_0.explorer_0.txtFileName.Text;
                    BackgroundWorker arg_82_0 = this.backgroundWorker_0;
                    if (runWorkerCompletedEventHandler == null)
                    {
                        runWorkerCompletedEventHandler = new RunWorkerCompletedEventHandler(this.method_1);
                    }
                    arg_82_0.RunWorkerCompleted += runWorkerCompletedEventHandler;
                    switch (this.genum0_0)
                    {
                        case GEnum0.const_0:
                            this.class2_0.explorer_0.method_5();
                            @class.stream_0 = new ab(this.class2_0.explorer_0._smb, this.class2_0.explorer_0._service, this.class2_0.explorer_0._remotePathManager.method_2() + "\\" + @class.string_0, 1);
                            break;
                        case GEnum0.const_1:
                            @class.stream_0 = this.class2_0.explorer_0._isolatedStore.OpenFile("content\\" + this.class2_0.explorer_0._localPathManager.method_2() + "\\" + @class.string_0, (FileMode)4);
                            break;
                        case GEnum0.const_2:
                            if (this.class2_0.explorer_0._skyDriveManager == null)
                            {
                                MessageBox.Show("Please, sign in on SkyDrive.");
                                return;
                            }
                            break;
                    }
                    this.backgroundWorker_0.DoWork += (new DoWorkEventHandler(@class.method_0));
                    this.backgroundWorker_0.RunWorkerAsync();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                this.class2_0.explorer_0.RenameCanvas.Visibility = (Visibility)1;
                this.class2_0.explorer_0.pivotLocation.Visibility = (Visibility)(0);
                this.class2_0.explorer_0.method_1();
            }
            private void method_1(object sender, RunWorkerCompletedEventArgs e)
            {
                this.class2_0.explorer_0.Dispatcher.BeginInvoke(new Action(this.method_2));
            }
            private void method_2()
            {
                //MessageBox.Show("File successfully copied");
                Prompt.PromptMessage("文件已经成功拷贝！");
                this.class2_0.explorer_0.pbReading.Visibility = (Visibility)(1);
                this.class2_0.explorer_0.pbReading.IsIndeterminate = false;
                this.class2_0.explorer_0.pivotLocation.IsEnabled = (true);
                this.class2_0.explorer_0.ApplicationBar.IsVisible = true;
                this.class2_0.explorer_0.method_11();
            }
        }


        #endregion ******************************内部类******************************

        public ExplorerPage()
        {
            InitializeComponent();
            this.lstBoxMachine.Height = Application.Current.Host.Content.ActualHeight - 250;
            this.lstBoxLocal.Height = Application.Current.Host.Content.ActualHeight - 250;
            this.Loaded += ExplorerPage_Loaded;
        }

        void ExplorerPage_Loaded(object sender, RoutedEventArgs e)
        {
            Func<ConnectionViewModel, bool> func = null;
            if (this._newPage)
            {
                this.method_1();
                if (base.NavigationContext.QueryString.ContainsKey("ConnectionName"))
                {
                    if (!App.ViewModel.IsDataLoaded)
                    {
                        App.ViewModel.LoadData();
                        App.ViewModel.LoadSettings();
                    }
                    MainViewModel arg_75_0 = App.ViewModel;
                    IEnumerable<ConnectionViewModel> arg_70_0 = App.ViewModel.Items;
                    if (func == null)
                    {
                        func = new Func<ConnectionViewModel, bool>(this.method_18);
                    }
                    arg_75_0.SelectedConnection = Enumerable.FirstOrDefault<ConnectionViewModel>(arg_70_0, func);
                    if (App.ViewModel.SelectedConnection == null)
                    {
                        Prompt.PromptMessage("连接未找到！");
                        base.NavigationService.GoBack();
                        return;
                    }
                }
                if (!base.NavigationContext.QueryString.ContainsKey("local"))
                {
                    this.method_4();
                }
                else
                {
                    this.method_7(this._localPathManager.method_2());
                    this.pivotLocation.Items.Remove(this.pivotRemote);
                    this._isLocalMode = true;
                }
                if (!App.ViewModel.AppSettings.EnableSkyDrive)
                {
                    //this.pivotLocation.get_Items().Remove(this.pivotSkyDrive);
                }
                this._newPage = false;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!this._newPage && !base.NavigationContext.QueryString.ContainsKey("local"))
            {
                this.method_5();
            }
            base.OnNavigatedTo(e);
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (this._smb == null)
            {
                return;
            }
            //if (MessageBox.Show("Are you sure?", "Disconnect", (MessageBoxButton)1) == (MessageBoxResult)2)
            if (MessageBox.Show("确认断开连接？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
            {
                e.Cancel = (true);
                return;
            }
            if (this._IsSignButtonClicked)
            {
                base.NavigationService.GoBack();
            }
            this._smb.k();
            if (this._smb.p())
            {
                this._smb.j();
            }
        }

        private void lstBoxLocal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void lstBoxMachine_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                GClass1 gClass = e.AddedItems[0] as GClass1;
                if (gClass.IsDirectory)
                {
                    if (gClass.FileName != "..")
                    {
                        this._remotePathManager.method_0(gClass.FileName);
                    }
                    else
                    {
                        this._remotePathManager.method_1();
                    }
                    this.method_6(this._service, this._remotePathManager.method_2());
                }
            }
        }




        private void _smb_ConnectedCompleted(object sender, EventArgs e)
        {
            if (this._smb.p())
            {
                this._service = App.ViewModel.SelectedConnection.Service;
                this._smb.i(new EventHandler<a6>(this._smb_ListFolderCompleted));
                this.method_6(this._service, this._remotePathManager.method_2());
                this.method_7(this._localPathManager.method_2());
                base.ApplicationBar.IsVisible = (true);
                this.pivotLocation.IsEnabled = (true);
                return;
            }
            if (!this._smb.l())
            {
                //MessageBox.Show(this._smb.m());
                MessageBox.Show("连接设备失败，请检查网络及其目标地址是否存在！");
                base.NavigationService.GoBack();
            }
        }

        private void _smb_ListFolderCompleted(object object_0, a6 a6_0)
        {
            if (this._smb.m() == null)
            {
                try
                {
                    List<aw> list = a6_0.d();
                    IEnumerable<aw> arg_3E_0 = list;
                    IOrderedEnumerable<aw> aws1 = arg_3E_0.Where<aw>((aw aw_0) =>
                    {
                        if (!aw_0.b)
                        {
                            return false;
                        }
                        return aw_0.a != ".";
                    }).OrderBy<aw, string>((aw aw_0) => aw_0.a);
                    IOrderedEnumerable<aw> aws2 = from aw_0 in arg_3E_0 where !aw_0.b orderby aw_0.a select aw_0;
                    this._remoteList.Clear();
                    using (IEnumerator<aw> enumerator = aws1.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            aw current = enumerator.Current;
                            if (current.a != ".." || (current.a == ".." && a6_0.c() != null))
                            {
                                GClass1 gClass = new GClass1();
                                gClass.FileName = current.a;
                                gClass.ImageSrc = "/Images/folder.png";
                                gClass.IsDirectory = true;
                                gClass.Path = a6_0.c();
                                this._remoteList.Add(gClass);
                            }
                        }
                    }
                    using (IEnumerator<aw> enumerator2 = aws2.GetEnumerator())
                    {
                        while (enumerator2.MoveNext())
                        {
                            aw current2 = enumerator2.Current;
                            GClass1 gClass2 = new GClass1();
                            gClass2.FileName = current2.a;
                            gClass2.ImageSrc = "/Images/file.png";
                            gClass2.Path = a6_0.c();
                            gClass2.FileSize = current2.c;
                            this._remoteList.Add(gClass2);
                        }
                    }
                    this.txtLocation.Text = (string.Concat(new string[] { "\\\\", App.ViewModel.SelectedConnection.Host, "\\", this._service, "\\", a6_0.c() }));
                    this.lstBoxMachine.ItemsSource = (this._remoteList);
                    this.pbReading.Visibility = Visibility.Collapsed;// (Visibility)(1);
                    this.pbReading.IsIndeterminate = false;
                    this.pivotLocation.IsEnabled = (true);
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    base.NavigationService.GoBack();
                    return;
                }
            }
            if (!this._smb.l())
            {
                MessageBox.Show(this._smb.m());
                base.NavigationService.GoBack();
            }
        }

        #region 方法
        private void method_2(EventHandler eventHandler_0, EventHandler eventHandler_1)
        {
            GClass6.smethod_0(base.ApplicationBar);
            GClass6.smethod_1(base.ApplicationBar, "appbar.ok.rest.png", "确认", eventHandler_0);
            GClass6.smethod_1(base.ApplicationBar, "appbar.cancel.rest.png", "取消", eventHandler_1);
        }
        private void method_3(EventHandler eventHandler_0)
        {
            GClass6.smethod_0(base.ApplicationBar);
            GClass6.smethod_1(base.ApplicationBar, "appbar.cancel.rest.png", "取消", eventHandler_0);
        }
        private void method_4()
        {
            this._smb = new aa();
            this._smb.h(new EventHandler(this._smb_ConnectedCompleted));
            this._smb.i(App.ViewModel.SelectedConnection.Host, App.ViewModel.SelectedConnection.User, App.ViewModel.SelectedConnection.Password);
        }
        private void method_5()
        {
            if (!this._smb.n().k())
            {
                this._smb.h(App.ViewModel.SelectedConnection.Host, App.ViewModel.SelectedConnection.User, App.ViewModel.SelectedConnection.Password);
            }
        }
        private void method_6(string string_0, string string_1)
        {
            this.pbReading.Visibility = (Visibility)(0);
            this.pbReading.IsIndeterminate = true;
            this.txtLocation.Text = ("正在加载目录文件...");
            this.lstBoxMachine.ItemsSource = (null);
            this.pivotLocation.IsEnabled = (false);
            this._smb.h(string_0, string_1);
        }
        private void method_7(string string_0)
        {
            ExplorerPage.Class1 @class = new ExplorerPage.Class1();
            @class.string_0 = string_0;
            @class.explorer_0 = this;
            this.txtLocalLocation.Text = ("Reading...");
            this.lstBoxLocal.ItemsSource = (null);
            this.pivotLocation.IsEnabled = (false);
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += (new DoWorkEventHandler(@class.method_0));
            backgroundWorker.RunWorkerCompleted += (new RunWorkerCompletedEventHandler(@class.method_1));
            backgroundWorker.RunWorkerAsync();
        }

        private void method_8(string string_0)
        {
            this.pbReading.Visibility = (Visibility)(0);
            this.pbReading.IsIndeterminate = true;
            //this.txtSkyLocation.Text = ("Reading...");
            //this.lstBoxSky.ItemsSource = (null);
            this.pivotLocation.IsEnabled = (false);
            this._skyList.Clear();
            if (!string.IsNullOrEmpty(string_0) && !(string_0 == "me/skydrive"))
            {
                GClass2 gClass = new GClass2();
                gClass.Id = this._skyPathManager.method_4();
                gClass.FileName = "..";
                gClass.ImageSrc = "/Images/folder.png";
                gClass.IsDirectory = true;
                this._skyList.Add(gClass);
                this._skyDriveManager.method_9(string_0 + "/files");
                return;
            }
            this._skyDriveManager.method_9("me/skydrive/files");
        }

        private int method_9(string string_0)
        {
            long length;
            using (IsolatedStorageFileStream isolatedStorageFileStream = this._isolatedStore.OpenFile(string_0, (FileMode)3))
            {
                length = isolatedStorageFileStream.Length;
            }
            return (int)length;
        }
        private GEnum0 method_10()
        {
            return this.pivotLocation.SelectedIndex + (this._isLocalMode ? GEnum0.const_1 : GEnum0.const_0);
        }
        private void method_11()
        {
            switch (this.method_10())
            {
                case GEnum0.const_0:
                    this.method_6(this._service, this._remotePathManager.method_2());
                    return;
                case GEnum0.const_1:
                    this.method_7(this._localPathManager.method_2());
                    return;
                case GEnum0.const_2:
                    this.method_8(this._skyPathManager.method_4());
                    return;
                default:
                    return;
            }
        }
        private void method_12(ListBox listBox_0)
        {
            if (listBox_0.ItemsSource == null)
            {
                return;
            }
            IEnumerator enumerator = listBox_0.ItemsSource.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    GClass1 gClass = (GClass1)enumerator.Current;
                    gClass.Checked = false;
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
        }

        private List<GClass1> method_13(ListBox listBox_0)
        {
            IEnumerable<GClass1> gClass1s = listBox_0.ItemsSource.Cast<GClass1>().Where<GClass1>((GClass1 gclass1_0) =>
            {
                if (!gclass1_0.Checked)
                {
                    return false;
                }
                return gclass1_0.FileName != "..";
            });
            return gClass1s.ToList<GClass1>();
        }

        private void method_14()
        {
            switch (this.method_10())
            {
                case GEnum0.const_0:
                    this._clipboard = new GClass0(GEnum0.const_0);
                    this._clipboard.method_3(this._remotePathManager.method_2());
                    this._clipboard.method_5(this.method_13(this.lstBoxMachine));
                    return;
                case GEnum0.const_1:
                    this._clipboard = new GClass0(GEnum0.const_1);
                    this._clipboard.method_3(this._localPathManager.method_2());
                    this._clipboard.method_5(this.method_13(this.lstBoxLocal));
                    return;
                case GEnum0.const_2:
                    this._clipboard = new GClass0(GEnum0.const_2);
                    //this._clipboard.method_5(this.method_13(this.lstBoxSky));
                    return;
                default:
                    return;
            }
        }

        private void method_15()
        {
            if (this.method_10() == GEnum0.const_2)
            {
                if (this._clipboard.method_4().Any<GClass1>((GClass1 gclass1_0) => gclass1_0.IsDirectory) && MessageBox.Show("You have selected directories to be deleted, note that ShareFolder not show OneNote notes inside directories, and it's possible that one of them contains notes. Do you want to continue?", "ATTENTION", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                {
                    return;
                }
            }
            this.StateCanvas.Visibility = (Visibility)(0);
            this.pivotLocation.Visibility = (Visibility)(1);
            this.method_3(new EventHandler(this.btnCancelPaste_Click));
            this.lbStatus.Text = ("Deleting files..");
            this._iscopying = false;
            this._dm = new GClass3(this._clipboard, this._smb, this._service, this._isolatedStore);
            //this._dm.method_3(this.btnSignSkyDrive.Session);
            PhoneApplicationService.Current.UserIdleDetectionMode = (IdleDetectionMode)1;
            this._dm.method_4().RunWorkerCompleted += (new RunWorkerCompletedEventHandler(this.BW_PasteCompleted));
            this._dm.method_5();
        }

        private void method_16(string string_0)
        {
            this.RenameCanvas.Visibility = (Visibility)(0);
            this.pivotLocation.Visibility = (Visibility)(1);
            this.method_2(new EventHandler(this.btnRename_Click), new EventHandler(this.btnCancelRename_Click));
            this.txtFileName.Text = (string_0);
            this.txtFileName.Focus();
        }

        private void method_17(bool bool_0)
        {
            ListBox listBox = null;
            switch (this.method_10())
            {
                case GEnum0.const_0:
                    listBox = this.lstBoxMachine;
                    break;
                case GEnum0.const_1:
                    listBox = this.lstBoxLocal;
                    break;
                case GEnum0.const_2:
                    //listBox = this.lstBoxSky;
                    break;
            }
            if (listBox.ItemsSource == null)
            {
                return;
            }
            IEnumerator enumerator = listBox.ItemsSource.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    GClass1 gClass = (GClass1)enumerator.Current;
                    if (!bool_0 || (bool_0 && !gClass.IsDirectory))
                    {
                        gClass.Checked = true;
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
        }



        private bool method_18(ConnectionViewModel connectionViewModel_0)
        {
            return connectionViewModel_0.Name.ToUpper() == base.NavigationContext.QueryString["ConnectionName"];
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (this.txtFileName.Text == "")
            {
                MessageBox.Show("Please enter new file name");
                return;
            }
            try
            {
                switch (this.method_10())
                {
                    case GEnum0.const_0:
                        {
                            ushort a_ = this._smb.i(this._service);
                            string text = this._fileToRename.Path + "\\" + this.txtFileName.Text;
                            this._smb.h(a_, this._fileToRename.Path + "\\" + this._fileToRename.FileName, text);
                            this._smb.h(a_);
                            break;
                        }
                    case GEnum0.const_1:
                        {
                            string text = this._fileToRename.Path + "\\" + this.txtFileName.Text;
                            this._isolatedStore.MoveFile("content\\" + this._fileToRename.Path + "\\" + this._fileToRename.FileName, "content\\" + text);
                            break;
                        }
                    case GEnum0.const_2:
                        this._skyDriveManager.method_17((this._fileToRename as GClass2).Id, this.txtFileName.Text);
                        return;
                }
                this.method_11();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.RenameCanvas.Visibility = (Visibility)(1);
                this.pivotLocation.Visibility = (Visibility)(0);
                this.method_1();
            }
        }

        private void btnCancelPaste_Click(object sender, EventArgs e)
        {
            if (this._iscopying)
            {
                if (this._pm != null && this._pm.method_8().IsBusy)
                {
                    this._pm.method_12();
                    return;
                }
            }
            else
            {
                this._dm.method_6();
            }
        }
        #endregion 方法


        #region 初始化ApplicationBar
        private void method_1()
        {
            if (base.ApplicationBar == null)
            {
                base.ApplicationBar = (new ApplicationBar());
                base.ApplicationBar.Opacity = 0.8;
            }
            GClass6.smethod_0(base.ApplicationBar);
            GClass6.smethod_1(base.ApplicationBar, "appbar.back.rest.png", "返回", new EventHandler(this.btnBack_Click));
            GClass6.smethod_1(base.ApplicationBar, "appbar.copy.rest.png", "复制", new EventHandler(this.btnCopy_Click));
            GClass6.smethod_1(base.ApplicationBar, "appbar.paste.rest.png", "粘贴", new EventHandler(this.btnPaste_Click));
            GClass6.smethod_1(base.ApplicationBar, "appbar.sync.rest.png", "刷新", new EventHandler(this.btnRefresh_Click));
            GClass6.smethod_2(base.ApplicationBar, "全选（含目录）", new EventHandler(this.mnuSelectAll_Click));
            GClass6.smethod_2(base.ApplicationBar, "全选（仅文件）", new EventHandler(this.mnuSelectFiles_Click));
            GClass6.smethod_2(base.ApplicationBar, "取消选中", new EventHandler(this.mnuDeselect_Click));
            GClass6.smethod_2(base.ApplicationBar, "创建目录", new EventHandler(this.mnuCreateDirectory_Click));
            GClass6.smethod_2(base.ApplicationBar, "删除选择的文件", new EventHandler(this.mnuDelete_Click));
            GClass6.smethod_2(base.ApplicationBar, "选择照片", new EventHandler(this.mnuLoadFromGallery_Click));
        }

        private void mnuCreateDirectory_Click(object sender, EventArgs e)
        {
            if (this.method_10() == GEnum0.const_2 && this._skyDriveManager == null)
            {
                MessageBox.Show("Please, sign in on SkyDrive");
                return;
            }
            this.CreateDirCanvas.Visibility = (Visibility)(0);
            this.pivotLocation.Visibility = (Visibility)(1);
            this.method_2(new EventHandler(this.btnCreateDir_Click), new EventHandler(this.btnCancelCreateDir_Click));
            this.txtDirName.Text = ("");
            this.txtDirName.Focus();
        }

        private void btnCreateDir_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtDirName.Text.Trim()))
            {
                //MessageBox.Show("Please, enter a directory name.");
                Prompt.PromptMessage("请输入目录名称");
                return;
            }
            try
            {
                switch (this.method_10())
                {
                    case GEnum0.const_0:
                        {
                            ushort a_ = this._smb.i(this._service);
                            this._smb.h(a_, this._remotePathManager.method_2() + "\\" + this.txtDirName.Text);
                            this._smb.h(a_);
                            break;
                        }
                    case GEnum0.const_1:
                        this._isolatedStore.CreateDirectory("content\\" + this._localPathManager.method_2() + "\\" + this.txtDirName.Text);
                        break;
                    case GEnum0.const_2:
                        this._skyDriveManager.method_11(this._skyPathManager.method_4(), this.txtDirName.Text);
                        return;
                }
                this.method_11();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.CreateDirCanvas.Visibility = (Visibility)(1);
                this.pivotLocation.Visibility = (Visibility)(0);
                this.method_1();
            }
        }
        private void btnCancelCreateDir_Click(object sender, EventArgs e)
        {
            this.CreateDirCanvas.Visibility = (Visibility)(1);
            this.pivotLocation.Visibility = (Visibility)(0);
            this.method_1();
        }

        private void mnuLoadFromGallery_Click(object sender, EventArgs e)
        {
            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += (new EventHandler<PhotoResult>(this.pct_Completed));
            photoChooserTask.Show();
        }

        private void pct_Completed(object object_0, PhotoResult photoResult_0)
        {
            ExplorerPage.Class2 @class = new ExplorerPage.Class2();
            @class.photoResult_0 = photoResult_0;
            @class.explorer_0 = this;
            if (@class.photoResult_0.TaskResult == (TaskResult)1)
            {
                ExplorerPage.Class3 class2 = new ExplorerPage.Class3();
                class2.class2_0 = @class;
                class2.genum0_0 = this.method_10();
                class2.backgroundWorker_0 = new BackgroundWorker();
                this.RenameCanvas.Visibility = (Visibility)(0);
                this.pivotLocation.Visibility = (Visibility)(1);
                this.method_2(new EventHandler(class2.method_0), new EventHandler(this.btnCancelRename_Click));
                this.txtFileName.Text = ("Photo_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".jpg");
                this.txtFileName.Focus();
            }
        }

        private void btnCancelRename_Click(object sender, EventArgs e)
        {
            this.RenameCanvas.Visibility = (Visibility)(1);
            this.pivotLocation.Visibility = (Visibility)(0);
            this.method_1();
        }

        private void mnuSelectAll_Click(object sender, EventArgs e)
        {
            this.method_17(false);
        }
        private void mnuSelectFiles_Click(object sender, EventArgs e)
        {
            this.method_17(true);
        }
        private void mnuDeselect_Click(object sender, EventArgs e)
        {
            ListBox listBox_ = null;
            switch (this.method_10())
            {
                case GEnum0.const_0:
                    listBox_ = this.lstBoxMachine;
                    break;
                case GEnum0.const_1:
                    listBox_ = this.lstBoxLocal;
                    break;
                case GEnum0.const_2:
                    //listBox_ = this.lstBoxSky;
                    break;
            }
            this.method_12(listBox_);
        }

        private void mnuDeleteLocal_Click(object sender, RoutedEventArgs e)
        {
            //            if (MessageBox.Show("Are you sure?", "Delete file", (MessageBoxButton)1) == (MessageBoxResult)1)
            if (MessageBox.Show("确认删除文件吗？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                this._clipboard = new GClass0(this.method_10());
                this._clipboard.method_5(new List<GClass1>());
                this._clipboard.method_4().Add((sender as MenuItem).DataContext as GClass1);
                this.method_15();
            }
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (this.method_10() == GEnum0.const_2 && this._skyDriveManager == null)
            {
                MessageBox.Show("Please, sign in on SkyDrive.");
                return;
            }
            this.method_11();
        }
        private void mnuDelete_Click(object sender, EventArgs e)
        {
            this.method_14();
            if (MessageBox.Show("确认删除文件吗？", AppResources.ApplicationTitle, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                this.method_15();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            switch (this.method_10())
            {
                case GEnum0.const_0:
                    if (this._remotePathManager.method_2() != null)
                    {
                        this.lstBoxMachine.SelectedIndex = (0);
                        return;
                    }
                    break;
                case GEnum0.const_1:
                    if (this._localPathManager.method_2() != null)
                    {
                        this.lstBoxLocal.SelectedIndex = (0);
                        return;
                    }
                    break;
                case GEnum0.const_2:
                    if (this._skyPathManager.method_4() != "me/skydrive")
                    {
                        //this.lstBoxSky.set_SelectedIndex(0);
                    }
                    break;
                default:
                    return;
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            this.method_14();
        }
        private void btnPaste_Click(object sender, EventArgs e)
        {
            if (this._clipboard != null && this._clipboard.method_0() == GEnum0.const_2)
            {
                this._clipboard.method_5((
                    from gclass1_0 in this._clipboard.method_4()
                    where !gclass1_0.IsDirectory
                    select gclass1_0).ToList<GClass1>());
            }
            if (this._IsTrial == "Trial")
            {
                if ((from gclass1_0 in this._clipboard.method_4() where gclass1_0.IsDirectory select gclass1_0).Count<GClass1>() > 0)
                {
                    //MessageBox.Show("In trial mode you cannot copy directories.");
                    Prompt.PromptMessage("不支持复制目录！");
                    return;
                }
            }
            if (this._clipboard == null || this._clipboard != null && this._clipboard.method_4().Count == 0)
            {
                //MessageBox.Show("Please, select files to copy.");
                Prompt.PromptMessage("未选择文件！");
                return;
            }
            if (this._IsTrial == "Trial")
            {
                if ((from gclass1_0 in this._clipboard.method_4() where gclass1_0.FileSize > 2097152 select gclass1_0).Count<GClass1>() > 0)
                {
                    MessageBox.Show("In trial mode you cannot copy files bigger than 2MB.");
                    return;
                }
            }
            if (this.method_10() == GEnum0.const_2 && this._clipboard.method_0() == GEnum0.const_2)
            {
                MessageBox.Show("You cannot copy files from SkyDrive to SkyDrive.");
                return;
            }
            if (this.method_10() == GEnum0.const_2 && this._skyDriveManager == null)
            {
                MessageBox.Show("You must sign in first.");
                return;
            }
            string str = null;
            switch (this.method_10())
            {
                case GEnum0.const_0:
                    {
                        str = this._remotePathManager.method_2();
                        break;
                    }
                case GEnum0.const_1:
                    {
                        str = this._localPathManager.method_2();
                        break;
                    }
                case GEnum0.const_2:
                    {
                        this._clipboard.method_5((from gclass1_0 in this._clipboard.method_4() where !gclass1_0.IsDirectory select gclass1_0).ToList<GClass1>());
                        str = this._skyPathManager.method_4();
                        break;
                    }
            }
            if (this._clipboard.method_2() == str && this._clipboard.method_0() == this.method_10())
            {
                //MessageBox.Show("Please, select a different path.");
                Prompt.PromptMessage("请选择不同目录进行操作！");
                return;
            }
            this.StateCanvas.Visibility = Visibility.Visible;
            this.pivotLocation.Visibility = Visibility.Collapsed;
            this.method_3(new EventHandler(this.btnCancelPaste_Click));
            this._iscopying = true;
            //this.lbStatus.Text = "Copying files...";
            this.lbStatus.Text = "正在复制文件...";
            this._pm = new GClass4(this._clipboard, this._smb, this._service, this._isolatedStore, str);
            this._pm.method_5(this.method_10());
            //this._pm.method_7(this.btnSignSkyDrive.Session);
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            this._pm.method_3(base.Dispatcher);
            this._pm.method_10(this.lbStatus);
            this._pm.method_8().ProgressChanged += new ProgressChangedEventHandler(this.BW_PasteFilesProgressChange);
            this._pm.method_8().RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.BW_PasteCompleted);
            this._pm.method_11();
        }

        private void BW_PasteFilesProgressChange(object sender, ProgressChangedEventArgs e)
        {
            this.pbPaste.Value = ((double)e.ProgressPercentage);
        }
        private void BW_PasteCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.StateCanvas.Visibility = (Visibility)(1);
            this.pivotLocation.Visibility = (Visibility)(0);
            this.pbPaste.Value = (0.0);
            if (!this._isLocalMode)
            {
                if (e.Error != null && !this._smb.l())
                {
                    MessageBox.Show(e.Error.Message);
                }
                else
                {
                    if (!this._smb.l())
                    {
                        this._clipboard = null;
                        this.method_12(this.lstBoxLocal);
                        this.method_12(this.lstBoxMachine);
                        //this.method_12(this.lstBoxSky);
                        this.method_11();
                    }
                }
            }
            else
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                }
                else
                {
                    this._clipboard = null;
                    this.method_12(this.lstBoxLocal);
                    //this.method_12(this.lstBoxSky);
                    this.method_11();
                }
            }
            this.method_1();
            PhoneApplicationService.Current.UserIdleDetectionMode = (IdleDetectionMode)0;
        }
        #endregion 初始化ApplicationBar

        #region 右键菜单事件
        private void mnuSaveRingTone_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                GClass1 gClass = (sender as MenuItem).DataContext as GClass1;
                SaveRingtoneTask saveRingtoneTask = new SaveRingtoneTask();
                saveRingtoneTask.Source = (new Uri("isostore:/content\\" + gClass.Path + "\\" + gClass.FileName));
                saveRingtoneTask.Completed += (new EventHandler<TaskEventArgs>(this.saveRingtoneChooser_Completed));
                saveRingtoneTask.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void saveRingtoneChooser_Completed(object sender, TaskEventArgs e)
        {
            switch (e.TaskResult)
            {
                case (TaskResult)0:
                    MessageBox.Show("Ringtone could not be saved.");
                    break;
                case (TaskResult)1:
                    MessageBox.Show("Ringtone saved!");
                    break;
            }
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
        }


        private void mnuView_Click(object sender, RoutedEventArgs e)
        {
            GClass1 dataContext = (sender as MenuItem).DataContext as GClass1;
            //base.NavigationService.Navigate(new Uri(string.Concat("/Pages/ViewImage.xaml?filename=content\\", dataContext.Path, @"\", dataContext.FileName), UriKind.Relative));
            if (dataContext == null)
                return;
            string json = JSONHelper.Serialize<GClass1>(dataContext);
            if (string.IsNullOrWhiteSpace(json))
                return;

            base.NavigationService.Navigate(new Uri(string.Concat("/Pages/ViewImage.xaml?context=", Uri.EscapeDataString(json)), UriKind.Relative));

        }
        private void mnuPlay_Click(object sender, RoutedEventArgs e)
        {
            GClass1 dataContext = (sender as MenuItem).DataContext as GClass1;
            MediaPlayerLauncher mediaPlayerLauncher = new MediaPlayerLauncher()
            {
                Media = new Uri(string.Concat("content\\", dataContext.Path, "\\", dataContext.FileName), UriKind.Relative),
                Location = MediaLocationType.Data,
                Controls = MediaPlaybackControls.All
            };
            mediaPlayerLauncher.Show();
        }

        private void mnuSaveToGallery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                GClass1 gClass = (sender as MenuItem).DataContext as GClass1;
                using (IsolatedStorageFileStream isolatedStorageFileStream = this._isolatedStore.OpenFile("content\\" + gClass.Path + "\\" + gClass.FileName, (FileMode)3))
                {
                    MediaLibrary mediaLibrary = new MediaLibrary();
                    mediaLibrary.SavePicture(gClass.FileName, isolatedStorageFileStream);
                }
                MessageBox.Show("Successfully saved image");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void mnuFileLocal_Opened(object sender, RoutedEventArgs e)
        {
            ContextMenu contextMenu = sender as ContextMenu;
            GClass1 gClass = contextMenu.DataContext as GClass1;
            if (gClass.FileName == "..")
            {
                contextMenu.IsOpen = false;
            }
        }

        private void mnuRename_Click(object sender, RoutedEventArgs e)
        {
            this._fileToRename = ((sender as MenuItem).DataContext as GClass1);
            this.method_16(this._fileToRename.FileName);
        }

        private void mnuRemoteOptions_Opened(object sender, RoutedEventArgs e)
        {
            ContextMenu contextMenu = sender as ContextMenu;
            GClass1 gClass = contextMenu.DataContext as GClass1;
            if (gClass.FileName == "..")
            {
                contextMenu.IsOpen = false;
            }
        }
        #endregion 右键菜单事件

        #region 将手机上的书籍导入至书架
        private void mnuImportReader_Click(object sender, RoutedEventArgs e)
        {
            GClass1 item = (sender as MenuItem).DataContext as GClass1;
            if (item == null)
            {
                Prompt.PromptMessage("请重新选择要导入的书籍！");
                return;
            }




            string content = string.Empty;
            bool isImportSuccess = false;

            #region 文件编码处理
            try
            {
                string filepath = string.Concat("content\\", item.Path, @"\", item.FileName);
                LogHelper.LogInfo(string.Format("文件路径：{0}", filepath));
                if (IsolatedStorageHelper.Instance.FileExists(filepath))
                {

                    Stream stream = IsolatedStorageHelper.Instance.ReadFile(filepath);
                    if (stream == null)
                    {
                        Prompt.PromptMessage("读取文件内容为空！");
                        return;
                    }

                    FileHelper fileHelper = new FileHelper();
                    byte[] bytes = fileHelper.StreamToBytes(stream);
                    if (!(bytes != null && bytes.Length > 0))
                    {
                        Prompt.PromptMessage("读取文件内容为空！");
                        return;
                    }
                    Encoding encoding = fileHelper.GetEncodingTypeByStream(stream);
                    if (encoding == null)
                    {
                        content = new Gb2312Encoding().GetString(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        content = encoding.GetString(bytes, 0, bytes.Length);
                    }

                    if (string.IsNullOrWhiteSpace(content))
                    {
                        Prompt.PromptMessage("文件内容为空！");
                    }
                }
                else
                {
                    Prompt.PromptMessage("文件未找到！");
                }
            }
            catch (Exception err)
            {
                Prompt.PromptMessage("出现错误：" + err.Message);
            }
            #endregion 文件编码处理

            string bookTitle = item.FileName;
            if (item.FileName.LastIndexOf('.') > -1)
                bookTitle = item.FileName.Substring(0, item.FileName.LastIndexOf('.'));
            string bookId = string.Concat("import_book_id_", bookTitle);

            #region 写入指定位置
            if (string.IsNullOrWhiteSpace(content))
            {
                Prompt.PromptMessage("书籍内容为空，不进入导入操作！");
                return;
            }
            try
            {
                string writeFilePath = string.Concat("Shared/ShellContent/", bookId);
                if (IsolatedStorageHelper.Instance.FileExists(writeFilePath))
                {
                    Prompt.PromptMessage("书架已有同名书籍！");
                    return;
                }

                IsolatedStorageHelper.Instance.WriteText(content, writeFilePath, true);
                isImportSuccess = true;

            }
            catch (Exception err)
            {
                Prompt.PromptMessage("文件未能成功导入书架！");
            }
            #endregion 写入指定位置

            #region 导入书架
            try
            {
                
                //导入书籍无封面
                BookList_M bookstore = new BookList_M();
                bookstore.int_IsDownload_M = 2;
                bookstore.int_ProgressValue_M = 0xbb;
                bookstore.str_Author_M = "本地导入";
                bookstore.str_Cover_M = "/Resource/BookCoverIsNull.png";                
                bookstore.str_Id_M = bookId;// string.Concat("/Shared/ShellContent/", item.FileName); ;
                bookstore.str_Title_M = bookTitle;
                bookstore.str_Size_M = item.Size;
                bookstore.str_TotalWords_M = content.Length.ToString();
                App.LocalBook.AddBookList(bookstore);
                Prompt.PromptMessage("恭喜，已将书籍导入书架！");
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
                Prompt.PromptMessage("文件未能成功导入书架！");
            }
            #endregion 导入书架
        }
        #endregion 将手机上的书籍导入至书架




    }
}